package com.qianfeng.zhangwei.blutooth;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * @说 明：
 * @项目名称：Sz1504Adv
 * @包 名： com.qianfeng.zhangwei.blutooth
 * @类 名： DeviceAdapter
 * @创 建人： zhangwei
 * @创建时间：2015-08-11 09:49
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class DeviceAdapter extends BaseAdapter {

    private List<BluetoothDevice> devices;
    private LayoutInflater inflater;

    public DeviceAdapter(List<BluetoothDevice> devices, Context context) {
        this.devices = devices;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adatper_device_layout, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        vh.addressTv.setText(devices.get(position).getAddress());
        vh.nameTv.setText(devices.get(position).getName());

        return convertView;
    }


    public static class ViewHolder {
        private TextView nameTv;
        private TextView addressTv;

        public ViewHolder(View itemView) {
            this.nameTv = (TextView) itemView.findViewById(R.id.device_name);
            this.addressTv = (TextView) itemView.findViewById(R.id.device_address);
        }
    }


}
